package com.brillilab.starter.controller.aio.kit;

import java.math.BigDecimal;
import java.util.List;

import com.brillilab.service.core.lab.ILabMemberPowerService;
import com.brillilab.service.core.lab.ILabMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.inventory.InventoryRecordEnum;
import com.brillilab.domain.enums.lab.LabMemberPowerEnum;
import com.brillilab.domain.pa.kit.KitModifyPa;
import com.brillilab.domain.pa.kit.KitSpecAddPa;
import com.brillilab.domain.pa.kit.ReagentSearchPa;
import com.brillilab.domain.po.inventory.ReagentInventory;
import com.brillilab.domain.po.kit.Kit;
import com.brillilab.domain.vo.kit.KitUsageCountVo;
import com.brillilab.domain.vo.kit.ReagentDetailsVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.core.inventory.IInventoryRecordService;
import com.brillilab.service.core.inventory.IReagentInventoryService;
import com.brillilab.service.core.kit.IKitService;
import com.brillilab.service.logic.kit.KitUsageLogic;
import com.brillilab.service.logic.kit.ReagentLogic;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import com.brillilab.starter.entity.resp.kit.KitIndexMsgResp;
import com.brillilab.starter.handler.RequestPower;

import javax.annotation.Resource;

/**
 * kit试剂Controller
 *
 * @author lj
 */
@RestController
@RequestMapping("/api/reagent")
public class ReagentController {

    @Resource
    private UsersLogic usersLogic;
    @Resource
    private ReagentLogic reagentLogic;
    @Resource
    private IKitService kitService;
    @Resource
    private IInventoryRecordService inventoryRecordService;
    @Resource
    private IReagentInventoryService reagentInventoryService;
    @Resource
    private KitUsageLogic kitUsageLogic;
    @Resource
    private ILabMemberService labMemberService;
    @Resource
    private ILabMemberPowerService labMemberPowerService;


    /**
     * 试剂列表页消息
     *
     * @param labId
     * @return
     */
    @RequestMapping(value = "/indexMsg", method = RequestMethod.GET)
    public ResponseVo indexMsg(@RequestHeader(name = StarterConstant.TOKEN) String token) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        KitUsageCountVo countVo = kitUsageLogic.getKitUsageCountVo(urs.getLabId(), urs.getLabMemberId());
        KitIndexMsgResp msgResp = new KitIndexMsgResp();
        msgResp.setHistoryUseNum(countVo.getHistory());
        msgResp.setInventoryNum(countVo.getUsage());
        msgResp.setReturnReagentNum(countVo.getList());
        return ResponseVo.success(msgResp);
    }

    /**
     * 获取首页试剂列表
     *
     * @param req 试剂搜索请求对象
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ResponseVo kitList(@RequestHeader(name = StarterConstant.TOKEN) String token,
                              @RequestBody ReagentSearchPa pa) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        pa.setLabId(urs.getLabId());
//		pa.setLabMemberId(urs.getLabMemberId());
        return ResponseVo.success(reagentLogic.kitList(pa, urs));
    }

    /**
     * 获取试剂详情
     *
     * @param kitId
     * @return
     */
    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public ResponseVo getKitDetails(@RequestHeader(name = StarterConstant.TOKEN) String token,
                                    @RequestParam Long kitId) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        if (!labMemberService.isOwner(urs.getLabId(), urs.getLabMemberId()) && !labMemberPowerService.isPower(urs.getLabId(), urs.getLabMemberId(), LabMemberPowerEnum.Power.REAGENT)) {
            kitService.assertIsLabKit(urs.getLabId(), kitId);
        }
        ReagentDetailsVo detailsVo = reagentLogic.getReagentDetails(kitId);
        return ResponseVo.success(detailsVo);
    }

    /**
     * 使用记录列表
     *
     * @param kitId
     * @return
     */
    @RequestMapping(value = "/useList", method = RequestMethod.GET)
    public ResponseVo useList(@RequestHeader(name = StarterConstant.TOKEN) String token, @RequestParam Long kitId,
                              @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10") Integer pageSize) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        kitService.assertIsLabKit(urs.getLabId(), kitId);
        return ResponseVo.success(inventoryRecordService.selectList(kitId, pageNum, pageSize));
    }

    /**
     * 删除试剂前判断是否有库存
     *
     * @param token
     * @param kitId
     * @return
     */
    @RequestPower(LabMemberPowerEnum.reagent)
    @RequestMapping(value = "/isInventory", method = RequestMethod.GET)
    public ResponseVo isInventory(@RequestHeader(name = StarterConstant.TOKEN) String token, @RequestParam Long kitId) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        kitService.assertIsLabKit(urs.getLabId(), kitId);
        int inventoryNum = 0;
        // 1、判断是否有库存
        List<ReagentInventory> inventoryList = reagentInventoryService.selectListByKitId(kitId);
        if (inventoryList != null && inventoryList.size() > 0) {
            inventoryNum = inventoryList.size();
        }
        return ResponseVo.successMap("inventoryNum", inventoryNum);
    }

    /**
     * 删除试剂
     *
     * @param kitId
     * @return
     */
    @AspectLog
    @RequestPower(LabMemberPowerEnum.reagent)
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public ResponseVo delete(@RequestHeader(name = StarterConstant.TOKEN) String token, @RequestParam Long kitId) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        kitService.assertIsLabKit(urs.getLabId(), kitId);
        reagentLogic.delete(kitId, urs.getLabMemberId());
        inventoryRecordService.add(urs, kitId, InventoryRecordEnum.DELETE);
        return ResponseVo.success();
    }

    /**
     * 修改试剂
     *
     * @param
     * @return
     */
    @AspectLog
    @RequestPower(LabMemberPowerEnum.reagent)
    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    public ResponseVo reagentModify(@RequestHeader(name = StarterConstant.TOKEN) String token,
                                    @RequestBody KitModifyPa kitModifyPa) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        kitService.assertIsLabKit(urs.getLabId(), kitModifyPa.getKitId());
        reagentLogic.reagentModify(kitModifyPa);
        inventoryRecordService.add(urs, kitModifyPa.getKitId(), InventoryRecordEnum.UPDATE);
        return ResponseVo.success();
    }

    /**
     * 获取试剂规格信息列表
     *
     * @param kitId kitId
     * @return
     */
    @RequestMapping(value = "/spec/info", method = RequestMethod.GET)
    public ResponseVo getReagentSpec(@RequestHeader(name = StarterConstant.TOKEN) String token,
                                     @RequestParam Long kitId) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        kitService.assertIsLabKit(urs.getLabId(), kitId);
        return ResponseVo.success(reagentLogic.getReagentSpec(kitId));
    }

    /**
     * 获取试剂的部分成分规格信息列表
     *
     * @param kitId
     * @return
     */
    @RequestMapping(value = "/component/spec", method = RequestMethod.GET)
    public ResponseVo getComponentSpec(@RequestHeader(name = StarterConstant.TOKEN) String token,
                                       @RequestParam Long kitId, @RequestParam Long[] spectIds) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        kitService.assertIsLabKit(urs.getLabId(), kitId);
        return ResponseVo.success(reagentLogic.getComponentSpec(kitId, spectIds));
    }

    /**
     * 新增试剂规格
     *
     * @param KitSpecAddPa
     * @return
     */
    @AspectLog
    @RequestMapping(value = "/spec/add", method = RequestMethod.POST)
    public ResponseVo addReagentSpec(@RequestHeader(name = StarterConstant.TOKEN) String token,
                                     @RequestBody KitSpecAddPa pa) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        kitService.assertIsLabKit(urs.getLabId(), pa.getKitId());
        pa.setLabId(urs.getLabId());
        pa.setCreatorId(urs.getLabMemberId());
        Long kitId = reagentLogic.addReagentSpec(pa);
        inventoryRecordService.add(urs, kitId, InventoryRecordEnum.ADD_SPEC);
        return ResponseVo.successMap("kitId", kitId);
    }

    /**
     * 删除试剂规格前判断该规格是否有库存
     *
     * @return
     */
    @RequestPower(LabMemberPowerEnum.reagent)
    @RequestMapping(value = "/spec/isInventory", method = RequestMethod.GET)
    public ResponseVo deleteSpecIsInventory(@RequestHeader(name = StarterConstant.TOKEN) String token,
                                            @RequestParam Long kitId, @RequestParam BigDecimal kitSize) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        kitService.assertIsLabKit(urs.getLabId(), kitId);
        Integer inventoryNum = reagentLogic.specIsInventory(urs.getLabMemberId(), kitId, kitSize);
        return ResponseVo.successMap("inventoryNum", inventoryNum);
    }

    /**
     * 删除试剂规格
     *
     * @param KitSpecAddPa
     * @return
     */
    @AspectLog
    @RequestPower(LabMemberPowerEnum.reagent)
    @RequestMapping(value = "/spec/delete", method = RequestMethod.GET)
    public ResponseVo deleteReagentSpec(@RequestHeader(name = StarterConstant.TOKEN) String token,
                                        @RequestParam Long kitId, @RequestParam BigDecimal kitSize) {
        UserInfoVo urs = usersLogic.getUserByToken(token);
        Kit kit = kitService.selectById(kitId);
        Assert.isTrue(kit != null && kit.getLabId().equals(urs.getLabId()), "统自带规格，不可删除");
        reagentLogic.deleteSpec(urs.getLabMemberId(), kitId, kitSize);
        inventoryRecordService.add(urs, kitId, InventoryRecordEnum.DELETE_SPEC);
        return ResponseVo.success();
    }

}
